package sk.gtug.hackatlon.drunkdroid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import sk.gtug.hackatlon.drunkdroid.MainActivity;

/**
 * @author Richard Mihalovič
 */
public class Activity1Canvas  extends View {
    private Paint paintCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Boolean gameStarted = false;
    private Long firstStart = null;

    private Long gameStartedMillis = 0L;
    private Long gameTimeMillis = 0L;

    private Boolean runOnlyOnce = false;

    private int posX;
    private int posY;

    private int circleX;
    private int circleY;

    private int circleXDir = 5;
    private int circleYDir = 5;

    private int bigCircleRadius = 0;
    private float bigCircleRadiusScale = 0F;
    private int smallCircleRadius = 0;

    private boolean endGame = false;

    private int speedScale = 3;

    public Activity1Canvas(Context context) {
        super(context);
    }

    public Activity1Canvas(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Activity1Canvas(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(firstStart == null)
            firstStart = SystemClock.elapsedRealtime();

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        int cx = getMeasuredWidth() / 2;
        int cy = getMeasuredHeight() / 2;
        float radius;

        if (cx > cy) {
            radius = (float) (cy * 0.9);
        } else {
            radius = (float) (cx * 0.9);
        }

        init((int) radius, cx, cy);

        // SCORE
        canvas.drawText(Long.toString(gameTimeMillis), 10, 100, paintText);

        if(endGame) {
            String endGameStr = "THE END";
            Integer _10 = (int)(width * 0.1);
            canvas.drawText(endGameStr, cx - _10, height / 2, paintText);
            return;
        }

        if(Math.abs(SystemClock.elapsedRealtime()-firstStart) < 3000){
            String endGameStr = Integer.toString((int)(3 - Math.round((SystemClock.elapsedRealtime()-firstStart) / 1000.)));
            Integer _10 = (int)(width * 0.1);
            canvas.drawText(endGameStr, cx - _10, height / 2, paintText);
            gameStarted = false;
        } else {
            gameStarted = true;

            if(gameStartedMillis.equals(0))
                gameStartedMillis = SystemClock.elapsedRealtime();

            gameTimeMillis += 1;
        }

        canvas.drawCircle(circleX, circleY, bigCircleRadius, paintCircle);
        canvas.drawCircle(posX, posY, smallCircleRadius, paintCircle);

        if(!isInCircle(circleX, circleY, bigCircleRadius - smallCircleRadius, posX, posY)){
            endGame = true;
            setBackgroundColor(Color.rgb(255, 0, 0));

            if ( MainActivity.mPlayer != null ) {
            	MainActivity.mPlayer.play(MainActivity.mPlayGlass, 1, 1, 1, 0, 1);
            }
            return;
        }

        Double deg2rad = Math.PI / 180;

        if(circleX > width - radius/2)
            circleXDir = -circleXDir;
        if(circleX <= radius/2)
            circleXDir = -circleXDir;
        if(circleY > height - radius/2)
            circleYDir = -circleYDir;
        if(circleY <= radius/2)
            circleYDir = -circleYDir;
    }

    private Boolean isInCircle(int cx, int cy, int radius, int x, int y) {
        Double dist = Math.sqrt(Math.pow(cx - x, 2) + Math.pow(cy - y, 2));
        return dist <= radius;
    }

    private void init(int radius, int cx, int cy){
        if(runOnlyOnce) return;

        paintCircle.setStyle(Paint.Style.STROKE);
        paintCircle.setStrokeWidth(3);
        paintCircle.setColor(Color.WHITE);

        paintText.setStyle(Paint.Style.STROKE);
        paintText.setStrokeWidth(3);
        paintText.setColor(Color.WHITE);
        paintText.setTextSize(34);

        runOnlyOnce = true;

        bigCircleRadius = (int) (bigCircleRadiusScale * radius);
        smallCircleRadius = (int) (0.1 * radius);

        posX = cx;
        posY = cy;

        circleX = cx;
        circleY = cy;
    }

    public void updatePosition(int dX, int dY){
        if(gameStarted) {
            posX += dX*speedScale;
            posY += dY*speedScale;

            circleX = circleX + circleXDir;
            circleY = circleY + circleYDir;
        }
    }

    public void setGameSpeedEasy(){
        this.circleXDir = 2;
        this.circleYDir = 2;

        this.speedScale = 2;
        this.bigCircleRadiusScale = 0.5F;
    }

    public void setGameSpeedNormal(){
        this.circleXDir = 5;
        this.circleYDir = 5;

        this.speedScale = 3;
        this.bigCircleRadiusScale = 0.5F;
    }

    public void setGameSpeedHard(){
        this.circleXDir = 5;
        this.circleYDir = 5;

        this.speedScale = 5;
        this.bigCircleRadiusScale = 0.4F;
    }

}